package com.my.lab.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.my.lab.R;

public class MutiActivity extends FragmentActivity {
    
    int mStackLevel = 1;
    Context mContext = null;
    ImageButton ibn1,ibn2,ibn3,ibn4,ibn5;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.muti_layout);
        mContext = this;  
        ibn1 = (ImageButton) findViewById(R.id.RadioButton0);
        ibn1.setOnClickListener(new View.OnClickListener() {
	    
	    @Override
	    public void onClick(View v) {
		FragmentManager fm = getSupportFragmentManager(); 
		if(fm.getBackStackEntryCount() > 0) {
		    fm.popBackStack(fm.getBackStackEntryAt(0).getId(),FragmentManager.POP_BACK_STACK_INCLUSIVE);
		}
	    }
	});
       
        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
	        
	        @Override
	        public void onClick(View v) {
	            addFragmentToStack();
	        }
	    });
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
	        
	        @Override
	        public void onClick(View v) {
	    	// TODO Auto-generated method stub
	            mStackLevel --;
	            FragmentManager fm = getSupportFragmentManager();
	            fm.popBackStack();
	        }
	    });
        if (savedInstanceState == null) {
            // Do first time initialization -- add initial fragment.
            Fragment newFragment = SonFragment.newInstance(mStackLevel);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.simple_fragment, newFragment).commit();
        } else {
            mStackLevel = savedInstanceState.getInt("level");
        }
    }
    
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("level", mStackLevel);
    }
    
    void addFragmentToStack () {
	mStackLevel ++;
	
	Fragment  newFragment = SonFragment.newInstance(mStackLevel);
	FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
	ft.replace(R.id.simple_fragment, newFragment);
	ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
	ft.addToBackStack(null);
	ft.commit();
    }
    
    public static class SonFragment  extends Fragment{
	int mNum;
	Context context;
	static SonFragment newInstance(int num) {
	    SonFragment s = new SonFragment();
	    Bundle args = new Bundle();
	    args.putInt("num", num);
	    s.setArguments(args);
	    
	    return s;
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    mNum = getArguments() != null ? getArguments().getInt("num") : 1 ;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	    View view = inflater.inflate(R.layout.son_layout, container, false);
	    View v = view.findViewById(R.id.tv);
	    ((TextView)v).setText("Fragment #"+mNum);
	    
	    return view;
	}
	
	
    }
}
